package com.ikdong.toodle.widget;

import java.util.ArrayList;
import java.util.List;
import com.ikdong.toodle.R;
import com.ikdong.toodle.activity.TaskAddActivity;
import com.ikdong.toodle.model.Task;
import com.ikdong.toodle.util.Constant;
import com.ikdong.toodle.util.TaskManager;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.LinearLayout;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.AdapterView;
import android.widget.ListView;

public final class TaskTabFragment extends BasicTabFrame {
	private TaskTabAdapter parentAdapter;
	private TaskManager manager;
	private TaskListAdapter adapter;
    private String tab;
    private List<Task> tasks;
    private String type;
    private ListView listView;
    private View emptyView;
    
    public static TaskTabFragment newInstance(TaskTabAdapter parentAdapter,String tab,List<Task> tasks,String type) {
    	TaskTabFragment fragment = new TaskTabFragment();
    	fragment.setTab(tab);
    	fragment.setTasks(tasks);
    	fragment.setType(type);
    	fragment.setParentAdapter(parentAdapter);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        manager = new TaskManager(this.getActivity());
        adapter = new TaskListAdapter(this.getActivity());
        adapter.setTabAdapter(parentAdapter);
    }
    
    @Override
    public void onResume(){
    	super.onResume();
        initTasksByView();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    	emptyView = inflater.inflate(R.layout.empty_list_task,container,false);
    	emptyView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    	emptyView.setVisibility(View.GONE);
    	listView = new ListView(getActivity());
    	listView.setTextFilterEnabled(true);
    	listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id){
				Intent intent = new Intent(getActivity(),TaskAddActivity.class);
				intent.putExtra(TaskAddActivity.OPERATION, TaskAddActivity.OP_UPDATE);
				intent.putExtra(TaskAddActivity.TID, listView.getAdapter().getItemId(position));
				startActivityForResult(intent,0);
			}			
		});
		listView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		registerForContextMenu(listView);
		
        LinearLayout layout = new LinearLayout(getActivity());
        layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        layout.addView(listView);
        layout.addView(emptyView);
        
        return layout;
    }
    
    @Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	    super.onCreateContextMenu(menu, v, menuInfo);
	    MenuInflater inflater = this.getActivity().getMenuInflater();
	    inflater.inflate(R.menu.task_list_menu, menu);
	    
	    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
	    menu.setHeaderTitle(adapter.getItem(info.position).getTitle());
	}
    
    @Override
	public boolean onContextItemSelected(android.view.MenuItem item) {
		final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	    switch (item.getItemId()) {
	        case R.id.tli_menu_delete:
	        	new AlertDialog.Builder(TaskTabFragment.this.getActivity())
		    	.setTitle(TaskTabFragment.this.getString(R.string.msg_alert_task_delete_title))
		    	.setMessage(TaskTabFragment.this.getString(R.string.msg_alert_task_delete_body))
		    	.setPositiveButton(TaskTabFragment.this.getString(R.string.lable_action_ok), new DialogInterface.OnClickListener() {
		    		public void onClick(DialogInterface dialog, int which) { 
			        	boolean flag = manager.deleteByStatus(info.id);
			        	if(flag){	        		
			        		removeItem(info.id);
			        	}	
		    		}
		    	})
		    	.setNegativeButton(TaskTabFragment.this.getString(R.string.lable_action_cancel), new DialogInterface.OnClickListener() {
		    		public void onClick(DialogInterface dialog, int which) {}
		    	})
		    	.show();
	            return true;
	        case R.id.tli_menu_edit:
	        	Intent intent = new Intent(this.getActivity(),TaskAddActivity.class);
				intent.putExtra(TaskAddActivity.OPERATION, TaskAddActivity.OP_UPDATE);
				intent.putExtra(TaskAddActivity.TID, info.id);
				startActivity(intent);
	            return true;
	        default:
	            return true;
	    }
	}


	@Override
	public void refresh() {
        initTasksByView();
	}
	
	private void removeItem(long id){
		for(int i=0; i<tasks.size(); i++){
			if(tasks.get(i).getId()==id){
				tasks.remove(i);
				break;
			}
		}
		
		parentAdapter.allDataChanged(id);
	}
	
	public void setTab(String tab){
		this.tab = tab;
	}
	
	public void setType(String type){
		this.type = type;
	}
	
	public void setTasks(List<Task> tasks){
		this.tasks = tasks;
	}
	
	public TaskListAdapter getAdapter(){
		return this.adapter;
	}	

    public void setParentAdapter(TaskTabAdapter parentAdapter) {
		this.parentAdapter = parentAdapter;
	}
	
	@Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }
	
	public void initTasksByView(){
		List<Task> ts = new ArrayList<Task>();
		if(Constant.TASK_VIEW_TAB_MAIN.equalsIgnoreCase(type)){
			ts = this.filterTaskByMain(tasks, tab);
		}else if(Constant.TASK_VIEW_TAB_FOLDER.equalsIgnoreCase(type)){
			ts = this.filterTaskByFolder(tasks, tab);
		}else if(Constant.TASK_VIEW_TAB_CONTEXT.equalsIgnoreCase(type)){
			ts = this.filterTaskByContext(tasks, tab);
		}else if(Constant.TASK_VIEW_TAB_GOAL.equalsIgnoreCase(type)){
			ts = this.filterTaskByGoal(tasks, tab);
		}else if(Constant.TASK_VIEW_TAB_DUEDATE.equalsIgnoreCase(type)){
			ts = this.filterTaskByDuedate(tasks, tab);
		}else if(Constant.TASK_VIEW_TAB_PRIORITY.equalsIgnoreCase(type)){
			ts = this.filterTaskByPriority(tasks, tab);
		}else if(Constant.TASK_VIEW_TAB_STATUS.equalsIgnoreCase(type)){
			ts = this.filterTaskByStatus(tasks, tab);
		}

		adapter.setTasks(ts);
		adapter.notifyDataSetChanged();
		if(adapter.getCount()==0){
			if(emptyView!=null){
				emptyView.setVisibility(View.VISIBLE);
				listView.setVisibility(View.GONE);
			}
		}else{
			emptyView.setVisibility(View.GONE);
			listView.setVisibility(View.VISIBLE);
		}
	}
}
